package seats;

import database.Transcation;
import java.sql.*;

public class Seat extends Transcation
{
	private short seatNum;
	private boolean isAvailable;
	private String classType;
	
	
	
	public Seat()
	{
		seatNum = -1;// we donot know which seat customer will take.
	}
	public Seat(short no, boolean status,String type)
	{
		System.out.println(no + "\t" + status+ "\t" + type);
		seatNum = no;
		isAvailable = status;
		//setClassType(type); 
		
	}
	
	public void setIsAvailable( boolean status)
	{
		isAvailable = status;
	}
	
	public String getClassType()
	{
		return classType;
	}
	
	
	public boolean getIsAvailable()
	{
		return (isAvailable);
	}
	
	
	//**************************************************************************************************
	// this function will look for an empty space whose class type is "sTpye".
	// if there is empty space then it will return it number.
	// otherwise, it will returns -1;
	
	public short getEmptySeat(String sType)
	{
		PreparedStatement state = null;
		ResultSet resultSet = null;
				
		try
		{
			//check if there is an empty space with class type == sThpe;
			
			//query to be executed.
			String query = "SELECT seatNum FROM seat WHERE isAvailable =? AND classType =? LIMIT 1";
			
			state = connect.prepareStatement(query);//connetion
			state.setBoolean(1,true);// substitute "?" in query with appropaite ,desired argurment.
			state.setString(2,sType);
			
			
			resultSet = state.executeQuery();
			
			
			while( resultSet.next())// there is an empty space, it will take the first one, LIMIT 1 
			{
				seatNum = resultSet.getShort(1);// GET result attribute from query.
				isAvailable = false;// the seat now is booked.(it is not available any more)
				this.classType = sType; 
			}
		}
		catch (SQLException sqlException)
		{
				System.out.println("SQLException: " + sqlException.getMessage());
				System.out.println("SQLState: " + sqlException.getSQLState());
				System.out.println("VendorError: " + sqlException.getErrorCode());
				sqlException.printStackTrace();	
		}//end of try, catch block
			
		return seatNum;
	}
	//*************************************************************************************************************
	
	public static void showAllSeat()
	{
		Statement state = null;
		ResultSet resultSet = null;
		ResultSetMetaData metaData = null;
		
		try
		{
			state = connect.createStatement();
			
			resultSet = state.executeQuery("SELECT * FROM seat");
			
			metaData = resultSet.getMetaData();
			
			int numOfColumn = metaData.getColumnCount();
			
			System.out.print("\n\n\t");
			for ( int i = 1; i <= numOfColumn; i++)//output clomnNames
				System.out.printf("%-8s\t",metaData.getColumnName(i));
			
			System.out.print("\n\n\t");
				
			while( resultSet.next())
			{
				for ( int i = 1; i <= numOfColumn; i++)
					System.out.printf("%-8s\t",resultSet.getObject(i));
				
					System.out.print("\n\t");
		    }
	     }
	     catch (SQLException sqlException)
		 {
			System.out.println("SQLException: " + sqlException.getMessage());
		    System.out.println("SQLState: " + sqlException.getSQLState());
			System.out.println("VendorError: " + sqlException.getErrorCode());
			sqlException.printStackTrace();		
		 }
		 
	  return ;
	}
	
	
	
	
	//*********************************************************************************************************
	// this function will update any seat to be available or not avaliable , depends on argumnets passed to it.
	// if state is false then available = false;
	// if status is true then available = true;
	// seatNum is number of seat to be updated

	public static void updateDatabase(short seatNum, boolean status)
	{	
		PreparedStatement state = null;
		ResultSet resultSet = null;
				
		try
		{
			String query = "UPDATE seat SET isAvailable =? WHERE seatNum =?";
			
			state = connect.prepareStatement(query);
			state.setBoolean(1,status);
			state.setShort(2,seatNum);
			state.executeUpdate();
			state.close();
		}
		catch (SQLException sqlException)
		{
				System.out.println("SQLException: " + sqlException.getMessage());
				System.out.println("SQLState: " + sqlException.getSQLState());
				System.out.println("VendorError: " + sqlException.getErrorCode());
				sqlException.printStackTrace();	
		}//end of try, catch
	}
	//***********************************************************************************************************
}